package com.ruoyi.merchant.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.domain.RescueTask;
import com.ruoyi.merchant.service.IRescueTaskService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 救援任务Controller
 * 
 * @author ruoyi
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/rescueTask")
public class RescueTaskController extends BaseController
{
    @Autowired
    private IRescueTaskService rescueTaskService;

    /**
     * 查询救援任务列表
     */
    @RequiresPermissions("merchant:rescueTask:list")
    @GetMapping("/list")
    public TableDataInfo list(RescueTask rescueTask)
    {
        startPage();
        List<RescueTask> list = rescueTaskService.selectRescueTaskList(rescueTask);
        return getDataTable(list);
    }

    /**
     * 导出救援任务列表
     */
    @RequiresPermissions("merchant:rescueTask:export")
    @Log(title = "救援任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RescueTask rescueTask)
    {
        List<RescueTask> list = rescueTaskService.selectRescueTaskList(rescueTask);
        ExcelUtil<RescueTask> util = new ExcelUtil<RescueTask>(RescueTask.class);
        util.exportExcel(response, list, "救援任务数据");
    }

    /**
     * 获取救援任务详细信息
     */
    @RequiresPermissions("merchant:rescueTask:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(rescueTaskService.selectRescueTaskById(id));
    }

    /**
     * 新增救援任务
     */
    @RequiresPermissions("merchant:rescueTask:add")
    @Log(title = "救援任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RescueTask rescueTask)
    {
        Integer userId = Math.toIntExact(SecurityUtils.getUserId());
        rescueTask.setUid(userId);  // 自动绑定用户ID
        rescueTask.setTime(new Date()); // 设置发起时间
        return toAjax(rescueTaskService.insertRescueTask(rescueTask));
    }

    /**
     * 修改救援任务
     */
    @RequiresPermissions("merchant:rescueTask:edit")
    @Log(title = "救援任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RescueTask rescueTask)
    {
        return toAjax(rescueTaskService.updateRescueTask(rescueTask));
    }

    /**
     * 删除救援任务
     */
    @RequiresPermissions("merchant:rescueTask:remove")
    @Log(title = "救援任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(rescueTaskService.deleteRescueTaskByIds(ids));
    }
}
